﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;
using System.Text;

namespace ExpertMIS.Service
{
    public partial class ExpertListService : System.Web.UI.Page
    {
        private ExpertBroker _expertBroker = new ExpertBroker();
        
        private string ReturnJSONExpression()
        {
            IList<ExpertEntity> expertList = GetExpertListByDomains();

            StringBuilder sb = new StringBuilder();
            sb.Append(SYS.Common.JSONSerializer.ObjectToJSON(expertList));

            return sb.ToString();
        }

        private string ReturnHTML()
        {
            ExpertBroker expertBroker = new ExpertBroker();
            IList<ExpertEntity> expertList = GetExpertListByDomains();

            StringBuilder sb = new StringBuilder();
            sb.Append("<table class='service-expert-list'>");

            int i = 0;

            foreach (ExpertEntity expert in expertList)
            {
                i++;
                StringBuilder sbDomain = new StringBuilder();
                foreach (DomainEntity domain in expertBroker.GetExpertDomains(expert.OID))
                {
                    if (sbDomain.Length > 0) sbDomain.Append(",");
                    sbDomain.Append(string.Format("<a href='{0}?oid={1}' alt='' target='_blank'>{2}</a>", ResolveClientUrl("~/Forums/DomainIntroduce.aspx"), domain.OID, domain.DomainName, domain.Remark));
                }

                sb.Append(string.Format("<tr class='{0}'>",i%2==0?"service-expert-tr2":"service-expert-tr1"));
                sb.Append(string.Format("<td style='width:80px;'><input type='checkbox' expert='{1}'/><a href='{0}{1}' target='_blank'>{2}</a></td><td style='width:100px;'>{3}</td><td style='width:200px;'>{4}</td>", ResolveUrl("~/Forums/ExpertView.aspx?oid="), expert.OID, expert.Name, sbDomain.ToString(), expert.Expertise));
                sb.Append("</tr>");
            }
            sb.Append("</table>");

            return sb.ToString();
        }

        private IList<ExpertEntity> GetExpertListByDomains()
        {
            List<String> listDomain = new List<string>();

            string choosedDomain = Request["chooseddomain"];
            string[] splitted = choosedDomain.Split(',');

            foreach (string s in splitted)
            {
                listDomain.Add(s);
            }

            IList<ExpertEntity> expertList = _expertBroker.GetExpertsByDomain(listDomain);
            return expertList;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            if ("json".Equals(Request["returntype"]))
            {
                Response.Write(ReturnJSONExpression());
            }
            else if ("html".Equals(Request["returntype"]))
            {
                Response.ContentType = "text/html";
                Response.Write(ReturnHTML());
            }
            Response.End();
        }
    }
}
